using System;
using System.Web.Security;
using Symbonix.Library.Web.Security.DAL.Repositories;
using Symbonix.Library.Web.Security.DAL.Entities;
namespace Symbonix.NorSoNet.Security.BLL.Membership
{

    public class Member : SystemUser
    {
        private IUserProfile iUserProfile;
        private Symbonix.NorSoNet.Security.BLL.Privacy.IPrivacyPolicy iPrivacyPolicy;

        protected Member() { }

        internal Member(IUser systemUser)
        {
            this.Comment = systemUser.Comment;
            this.CreationDate = systemUser.CreationDate;
            this.Email = systemUser.Email;
            this.FirstName = systemUser.FirstName;
            this.Id = systemUser.Id;
            this.IsApproved = systemUser.IsApproved;
            this.IsLockedOut = systemUser.IsLockedOut;
            this.LastActivityDate = systemUser.LastActivityDate;
            this.LastLockoutDate = systemUser.LastLockoutDate;
            this.LastLoginDate = systemUser.LastLoginDate;
            this.LastName = systemUser.LastName;
            this.LastPasswordChangedDate = systemUser.LastPasswordChangedDate;
            this.UserName = systemUser.UserName;
        }

        protected internal static new IUser GetInstance() { return new Member(); }

        public override IUser Create(string userName, string password, string email, string firstName, string lastName, string role, bool isApproved, out MembershipCreateStatus status)
        {
            status = MembershipCreateStatus.ProviderError;
            MembershipUser user = System.Web.Security.Membership.Providers[role].CreateUser(userName, password, email, string.Empty, string.Empty, isApproved, null, out status);

            Roles.Provider.AddUsersToRoles(new string[] { user.UserName }, new string[] { role });

            UserRepository ur = new UserRepository();
            // user.ProviderUserKey
            User dbUser = ur.GetByPrimaryKey(Int32.Parse(user.ProviderUserKey.ToString()));
            dbUser.FirstName = firstName;
            dbUser.LastName = lastName;
            ur.Update(dbUser);

            return base.LoadFromMembershipUser(user, firstName, lastName);
        }

        public override bool CreateNewPassword(Guid key, string userEmail, string newPassword)
        {
            // use email to get a membership user
            // use the membership user id to load a User entity
            //  decrypt username with password salt
            // if the username match, call BLL reset password with the new password

            throw new NotImplementedException();
        }
    }
}
